package cn.ulc.common.util;

import io.jsonwebtoken.Claims;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

public class UserUtils {
    private static final ThreadLocal<Object> THREAD_LOCAL_USER = new ThreadLocal<>();
    private static final ThreadLocal<Object> THREAD_LOCAL_COMPANY = new ThreadLocal<>();
    private static final ThreadLocal<Object> THREAD_LOCAL_SHOP = new ThreadLocal<>();
    private static final String KEY_USER = "user";

    /**
     * 根据header中的token获取用户ID
     *
     * @param httpServletRequest
     * @return 用户ID
     */
    public static Long getUserId(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJwtToken(token);
        Long userId = ConvertUtil.toLong(claims.get("userId")) ;
        return userId;
    }
    /**
     * 根据header中的token获取用户role
     */
    public static String getRole(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJwtToken(token);
        String role =(String) claims.get("role");
        return role;
    }
    /**
     * 根据header中的token获取当前商户Id  小程序端
     */
    public static Long getCompanyId(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJwtToken(token);
        Long companyId = ConvertUtil.toLong(claims.get("companyId")) ;
        return companyId;
    }
    /**
     * 根据header中的token获取当前门店Id  小程序端
     */
    public static Long getShopId(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJwtToken(token);
        Long shopId = ConvertUtil.toLong(claims.get("shopId")) ;
        return shopId;
    }
      /**
     * 根据header中的token获取用户ID
     *
     * @param httpServletRequest
     * @return 用户ID
     */
    public static UUID getClientId(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJwtToken(token);
        UUID clientId = UUID.fromString( ConvertUtil.toString(claims.get("userId")));
        return clientId;
    }


    /**
     * 设置用户信息
     *
     */
    public static void setUser(Object user) {
        THREAD_LOCAL_USER.set(user);
    }

    /**
     * 从threadlocal 获取用户
     */
    public static Object getUser() {
        return THREAD_LOCAL_USER.get();
    }

    public static void clearAllUserInfo() {
        THREAD_LOCAL_USER.remove();
    }
    //微信端
    /**
     * 设置商户信息
     *
     */
    public static void setCompany(Object user) {
        THREAD_LOCAL_COMPANY.set(user);
    }

    /**
     * 从threadlocal 获取商户
     */
    public static Object getCompany() {
        return THREAD_LOCAL_COMPANY.get();
    }

    public static void clearAllCompanyInfo() {
        THREAD_LOCAL_COMPANY.remove();
    }

    /**
     * 设置门店信息
     */
    public static void setShop(Object user) {
        THREAD_LOCAL_SHOP.set(user);
    }

    /**
     * 从threadlocal 获取商户
     */
    public static Object getShop() {
        return THREAD_LOCAL_SHOP.get();
    }

    public static void clearAllShopInfo() {
        THREAD_LOCAL_SHOP.remove();
    }
}
